/*
 *  Level.cpp
 *  GLRender
 *
 *  Created by Teseo on 20.05.10.
 *  Copyright 2010 __MyCompanyName__. All rights reserved.
 *
 */

#include "Level.h"
#include "Asteroid.h"
#include "World.h"

Level::Level(World *world):min(-100,-100,-100),max(100,100,100), startPosition(0,0,0)
{
	this->world=world;
	timeElapsed = 0.0;
	respawnStep = globals::RESPAWN_STEP; timeNextRespawn = 0.0; 
	
	minNumberOfEnemies = globals::MIN_ENEMIES; 
	maxNumberOfEnemies = globals::MAX_ENEMIES;
	
	number=1;
	
	objective=new WTarget(Point3d(0,0,-100));
	
	srand((unsigned)time(NULL));
}

Level*
Level::nextLevel(){
	return Level::getLevel(number+1, this->world);
}

void Level::step(const double tau, Point3d playerPosition){
	if (timeElapsed >= timeNextRespawn && world->getEnemies() < minNumberOfEnemies) {
		double deltaX=max.x()-min.x();
		double deltaY=max.y()-min.y();
		double deltaZ=max.z()-min.z();
		
		for(unsigned int i = world->getEnemies(); i < maxNumberOfEnemies; i++){
			double x=((double(rand())/RAND_MAX)*deltaX)+min.x();
			double y=((double(rand())/RAND_MAX)*deltaY)+min.y();
			double z=((double(rand())/RAND_MAX)*deltaZ)+min.z();
			
			Point3d pos(x,y,z);
			
			//if((pos-playerPosition).norm<100)
			//	pos+=playerPosition;
			
			Point3d v(0,0,0);
			Asteroid * asteroid = new Asteroid(pos,v);
			
			world->addMovingObject(asteroid);
		}
		timeNextRespawn = timeElapsed + respawnStep;
	} 
}